package com.tsd.system.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tsd.core.utils.HlpUtils;
import com.tsd.system.entity.SysDataAuthExt;
import com.tsd.system.entity.SysUser;
import com.tsd.core.utils.FieldUtils;
import com.tsd.utils.DataFieldAuthUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @ClassName: AuthViewFieldUtil
 * @Description: TODO
 * @Author: Hillpool
 * @Date: 2022/12/15 16:38
 * @Version: 1.0
 */
public class AuthViewFieldUtil {

    public interface GetOldDataCallback {
        Object get() throws Exception;
    }

    public static final String DATA_AUTH_CODE_CLIENT = "sysClientInfo";

    public static <T> void clearAuthViewField(String dataAuthCode, List<T> list, SysUser opUser) throws Exception {
        if (HlpUtils.isEmpty(dataAuthCode)) {
            return;
        }
        SysDataAuthExt authExt = DataFieldAuthUtil.getSysDataAuthField(dataAuthCode, opUser);
        if (authExt == null) {
            return;
        }
        List<String> fieldList = null;
        if (!HlpUtils.isEmpty(authExt.getAuth_field())) {
            fieldList = JSON.parseArray(authExt.getAuth_field(), String.class);
        }
        if (HlpUtils.isEmptyList(fieldList)) {
            return;
        }
        if (SysDataAuthExt.AUTH_MODE_NONE.equals(authExt.getAuth_mode())) {
            return;
        }
        if (SysDataAuthExt.AUTH_MODE_CREATOR.equals(authExt.getAuth_mode())) {
            for (Object data : list) {
                Object creatorSid = FieldUtils.getFieldValue(data, "creator_sid");
                if (HlpUtils.isEmpty(creatorSid) || opUser.getSid().equals(creatorSid)) {
                    continue;
                }
                FieldUtils.clearField(data, fieldList);
            }
            return;
        }
        if (SysDataAuthExt.AUTH_MODE_GROUP.equals(authExt.getAuth_mode())) {
            List<String> groupList = new ArrayList<>();
            if (!HlpUtils.isEmpty(opUser.getGroup_sid())) {
                groupList.add(opUser.getGroup_sid());
            }
            if (!HlpUtils.isEmpty(authExt.getGroup_sid())) {
                groupList.addAll(Arrays.asList(authExt.getGroup_sid().split(",")));
            }
            for (Object data : list) {
                Object groupSid = FieldUtils.getFieldValue(data, "group_sid");
                if (HlpUtils.isEmpty(groupSid) || groupList.contains(groupSid.toString())) {
                    continue;
                }
                FieldUtils.clearField(data, fieldList);
            }
        }
    }

    public static void clearAuthViewField(String dataAuthCode, Object object, SysUser opUser) throws Exception {
        clearAuthViewField(dataAuthCode, object, opUser, null);
    }

    public static void clearAuthViewField(String dataAuthCode, Object object, SysUser opUser, GetOldDataCallback callback) throws Exception {
        if (HlpUtils.isEmpty(dataAuthCode)) {
            return;
        }
        SysDataAuthExt authExt = DataFieldAuthUtil.getSysDataAuthField(dataAuthCode, opUser);
        if (authExt == null) {
            return;
        }
        List<String> fieldList = null;
        if (!HlpUtils.isEmpty(authExt.getAuth_field())) {
            fieldList = JSON.parseArray(authExt.getAuth_field(), String.class);
        }
        if (HlpUtils.isEmptyList(fieldList)) {
            return;
        }
        if (SysDataAuthExt.AUTH_MODE_NONE.equals(authExt.getAuth_mode())) {
            return;
        }
        if (SysDataAuthExt.AUTH_MODE_CREATOR.equals(authExt.getAuth_mode())) {
            Object creatorSid = FieldUtils.getFieldValue(object, "creator_sid");
            if (HlpUtils.isEmpty(creatorSid) || opUser.getSid().equals(creatorSid)) {
                return;
            }
            FieldUtils.clearField(object, fieldList);
            if (callback != null) {
                Object oldData = callback.get();
                FieldUtils.copyFieldValue(oldData, object, fieldList);
            }
        }
        if (SysDataAuthExt.AUTH_MODE_GROUP.equals(authExt.getAuth_mode())) {
            List<String> groupList = new ArrayList<>();
            if (!HlpUtils.isEmpty(opUser.getGroup_sid())) {
                groupList.add(opUser.getGroup_sid());
            }
            if (!HlpUtils.isEmpty(authExt.getGroup_sid())) {
                groupList.addAll(Arrays.asList(authExt.getGroup_sid().split(",")));
            }
            Object groupSid = FieldUtils.getFieldValue(object, "group_sid");
            if (HlpUtils.isEmpty(groupSid) || groupList.contains(groupSid.toString())) {
                return;
            }
            FieldUtils.clearField(object, fieldList);
            if (callback != null) {
                Object oldData = callback.get();
                FieldUtils.copyFieldValue(oldData, object, fieldList);
            }
        }
    }

    public static boolean checkAuthViewField(SysDataAuthExt authExt, JSONObject object, SysUser opUser) {
        if (authExt == null) {
            return true;
        }
        if (SysDataAuthExt.AUTH_MODE_NONE.equals(authExt.getAuth_mode())) {
            return true;
        }
        if (SysDataAuthExt.AUTH_MODE_CREATOR.equals(authExt.getAuth_mode())) {
            String creatorSid = object.getString("creator_sid");
            if (HlpUtils.isEmpty(creatorSid) || opUser.getSid().equals(creatorSid)) {
                return true;
            }
        }
        if (SysDataAuthExt.AUTH_MODE_GROUP.equals(authExt.getAuth_mode())) {
            List<String> groupList = new ArrayList<>();
            if (!HlpUtils.isEmpty(opUser.getGroup_sid())) {
                groupList.add(opUser.getGroup_sid());
            }
            if (!HlpUtils.isEmpty(authExt.getGroup_sid())) {
                groupList.addAll(Arrays.asList(authExt.getGroup_sid().split(",")));
            }
            String groupSid = object.getString("group_sid");
            return HlpUtils.isEmpty(groupSid) || groupList.contains(groupSid);
        }
        return false;
    }
}
